package com.mo.tools.http;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * description: HttpUtils
 * version:
 *
 * @date: 2019/09/19 09:11
 * @author: YangXingfu
 */
public class HttpUtils {

    /**
     * 日志记录
     */
    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    private static RequestConfig requestConfig = null;

    static {
        //  设置请求和传输超时时间
        requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
    }

    /**
     * 发送get请求
     * @param url 路径
     * @return json字符串
     */
    public static String httpGet(String url) {

        //  返回json数据
        String jsonResult = null;
        CloseableHttpClient client = HttpClients.createDefault();
        //  发送请求url地址
        HttpGet requestGet = new HttpGet(url);
        try {
            //  发送请求，得到返回结果
            CloseableHttpResponse response = client.execute(requestGet);

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //  读取服务器返回json字符串数组
                HttpEntity httpEntity = response.getEntity();
                //  将json字符串数组转成json字符串
                jsonResult = EntityUtils.toString(httpEntity, "UTF-8");
            }

        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }
        return jsonResult;
    }

    /**
     * post请求，传递String参数
     * @param url 请求地址
     * @param jsonParam 请求json参数
     * @return json数据
     */
    public static String httpPost(String url, String jsonParam) {
        //  json 返回值
        String jsonResult = null;
        //  创建post请求连接
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        //  设置请求和传输超时时间
        httpPost.setConfig(requestConfig);

        try {
            if (jsonParam != null) {
                StringEntity entity = new StringEntity(jsonParam, "UTF-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/x-www-form-urlencoded");
                httpPost.setEntity(entity);
            }


            //  发送请求
            CloseableHttpResponse response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                jsonResult = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return jsonResult;

    }

    /**
     * post请求，传递json参数
     * @param url 请求地址
     * @param jsonParam json参数
     * @return json结果
     */
    public static String httpPostJson(String url, String jsonParam) {
        //  json 返回值
        String jsonResult = null;
        //  创建post请求连接
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        //  设置请求和传输超时时间
        httpPost.setConfig(requestConfig);

        try {
            if (jsonParam != null) {
                StringEntity entity = new StringEntity(jsonParam, "UTF-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
            }


            //  发送请求
            CloseableHttpResponse response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                jsonResult = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return jsonResult;

    }

}
